<?php
/**
 * ============================================================================
 * Copyright (c) 2015-2018 贵州大师兄信息技术有限公司 All rights reserved.
 * siteַ: http://www.dsxcms.com
 * ============================================================================
 * @author:     David Song<songdewei@163.com>
 * @version:    v1.0.0
 * ---------------------------------------------
 * Date: 2018/9/18
 * Time: 下午5:36
 */

namespace App\Library\Mall\Item;

use App\Exceptions\ApiRequestException;
use App\Models\Item;
use App\Models\ItemCatlog;
use Illuminate\Http\Request;

trait ItemTrait
{
    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws ApiRequestException
     */
    public function get(Request $request){
        $itemid = $request->input('itemid',0);
        $item = Item::with(['content', 'images', 'shop'])->find($itemid);
        if ($item) {
            $item->incrementViews();
            $content = $item->content;
            if ($content) {
                if ($content->content) {
                    $content->content = cleanUpStyle($content->content);
                } else {
                    $content->content = '<p><img src="'.$item->image.'"></p>';
                }
            } else {
                $content->content = '<p><img src="'.$item->image.'"></p>';
            }

            $images = $item->images;

            $shop = $item->shop;
            $shop->item_count = Item::where(['shop_id'=>$item->shop_id])->count();

            $properties = $item->props;

            return ajaxReturn(compact('itemid', 'item', 'content', 'images', 'shop', 'properties'));
        }else {
            throw new ApiRequestException(trans('item.this item has been removed'), 403);
        }
    }

    /**
     * 批量获取商品信息
     * @return \Illuminate\Http\JsonResponse
     */
    public function batchget(Request $request){
        $offset = $request->input('offset', 0);
        $count  = $request->input('count', 20);

        $query = Item::with(['shop','images']);
        $keywords = $request->input('keywords');
        if ($keywords) {
            $query = $query->where('title', 'LIKE', "%$keywords%")->orWhereHas('shop', function ($query) use ($keywords){
                return $query->where('shop_name', 'LIKE', "%$keywords%");
            });
        }

        $catid = $request->input('catid');
        if ($catid) {
            $catIds = ItemCatlog::fetchAllChildIds($catid);
            $query = $query->whereIn('catid', $catIds);
        };

        $shop_id = $request->input('shop_id');
        if ($shop_id) $query = $query->where('shop_id', $shop_id);

        $sort = $request->input('sort');
        if ($sort == 'sold-desc') {
            $query = $query->orderByDesc('sold');
        } elseif ($sort == 'rate-desc') {
            $query = $query->orderByDesc('reviews');
        } else {
            $query = $query->orderByDesc('itemid');
        }

        $items = $query->offset($offset)->limit($count)->get();
        return ajaxReturn([
            'total_count'=>$query->count(),
            'offset'=>$offset,
            'count'=>$items->count(),
            'items'=>$items
        ]);
    }
}
